﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using plust.Clases;
using plust.Persistencia;
using FirstFloor.ModernUI.Windows.Controls;

namespace plust.Content
{
    /// <summary>
    /// Interaction logic for NuevoPagoDialog.xaml
    /// </summary>
    public partial class NuevoPagoDialog : UserControl
    {
        private PersistenciaDB persisitencia = new PersistenciaDB();
        private Factura factura;
        

        public NuevoPagoDialog()
        {
            InitializeComponent();
        }

        public NuevoPagoDialog(Factura factura) : this()
        {
            Keyboard.Focus(this.tbTipo);
            this.factura = factura;
        }

        private void btIngresarNuevoPago_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (int.Parse(this.tbMonto.Text) <= 0)
                {
                    new ModernDialog
                    {
                        Title = "ERROR",
                        Content = "Ingrese un valor que 0.",
                        Foreground = Brushes.Red,
                        FontWeight = FontWeights.SemiBold,
                    }.ShowDialog();
                }
                else
                {
                    Pago pago = new Pago();
                    pago.fecha_pago = DateTime.Now.ToString();
                    pago.id_factura = this.factura.id_factura;
                    pago.monto_pago = int.Parse(this.tbMonto.Text);
                    pago.descripcion = this.tbDescripcion.Text;
                    pago.tipo_pago = this.tbTipo.Text;
                    this.persisitencia.generarPago(pago);

                    new ModernDialog
                    {
                        Title = "CORRECTO",
                        Content = "Pago realizado correctamente.",
                    }.ShowDialog();

                    Window.GetWindow(this).Close();
                }

            } 
            catch (System.FormatException ex)
            {
                new ModernDialog
                {
                    Title = "ERROR",
                    Content = "Ingrese un numero.",
                    Foreground = Brushes.Red,
                    FontWeight = FontWeights.SemiBold,
                }.ShowDialog();
            }
        }

        private void btLimpiar_Click(object sender, RoutedEventArgs e)
        {
            this.tbDescripcion.Text = String.Empty;
            this.tbMonto.Text = String.Empty;
            this.tbTipo.Text = String.Empty;
        }
    }  
}
